1799B - Equalize by Divide - CodeForces Solution


brute force constructive algorithms greedy math

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_po?licy.hpp>

using namespace std;
// using namespace __gnu_pbds;

#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")

#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
//#define int long long

// template<typename T> using ordered_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
/*
order_of_key (k) : Number of items strictly smaller than k .
find_by_order(k) : K-th element in a set (counting from zero).
*/

typedef long long ll;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// template <class T> T rand(T l, T r) {
	// return (uniform_int_distribution<T>(l, r))(rng);
// }

#define int long long 
const int N = 5e5 + 2;
const int MOD = 1e9 + 7;


void solve() {
	int n;
	cin >> n;
	int a[n + 1];
	for(int i = 1; i <= n; ++i) {
		cin >> a[i];
	} 
	
	bool ok = 1;
	vector<pair<int,int>> res;
	while(ok && sz(res) <= 30 * n) {
		ok = false;
		int pos = min_element(a + 1, a + 1 + n) - a;
		for(int i = 1; i <= n; ++i) {
			if (a[pos] != a[i]) {
				a[i] = (a[i] + a[pos] - 1) / a[pos];
				res.pb({i, pos});
			}
		}
		
		for(int i = 1; i < n; ++i) {
			ok |= (a[i] != a[i+1]);
		}
	}
	
	for(int i = 1; i < n; ++i) {
		if (a[i] != a[i+1]) {
			cout << "-1\n";
			return;
		}
	}
	
	
	cout << sz(res) << '\n';
	for(auto [l, r] : res) {
		cout << l << ' ' << r << '\n';
	} 

}


signed main() {
	NFS;
	int test = 1;
	cin >> test;
	for(int i = 1; i <= test; ++i){
		solve();
	}
}







Comments

Submit
0 Comments
More Questions

1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant